<script setup lang="ts">
import type { FormInstance } from 'element-plus'
import { has } from 'lodash-es'
import { ElMessage } from 'element-plus'
import { useEntPageData, useInitForm } from '../createHelper'
import documentApi from '@/api/modules/document'

import useEntStore from '@/store/modules/ent'

const { currentEntData } = storeToRefs(useEntStore())

const { isCreate, id } = useEntPageData()

const { formData, initForm } = useInitForm()
const formRef = ref<FormInstance>()

watchEffect(() => {
  if (!isCreate.value && currentEntData.value.baseInfo) {
    formData.value = { ...currentEntData.value.baseInfo }
  }
})

function showAllName(item: any) {
  try {
    let name = ''
    let current = item
    name = item.Name
    while (has(current, 'Parent') && current.Parent) {
      current = current.Parent
      name = `${current.Name}/${name}`
    }
    return name
  }
  catch (e) {
    return ''
  }
}

function exportData() {
  ElMessage.info('正在导出，请稍等...')
  documentApi.GetEnterpriseToExcel({
    entID: id.value,
  }).then((res) => {
    const url = window.URL.createObjectURL(res as Blob)
    const a = document.createElement('a')
    a.href = url
    a.target = '_blank'
    a.click()
  }).catch(() => {
    ElMessage.error('导出失败，请重试。')
  })
}

onUnmounted(() => {
  formRef.value?.resetFields()
  initForm()
})
</script>

<template>
  <div class="mt-10">
    <el-descriptions title="企业信息" :column="2" border>
      <el-descriptions-item label="企业名称">
        {{ formData.Name }}
      </el-descriptions-item>
      <el-descriptions-item label="统一社会信用代码">
        {{ formData.CreditCode }}
      </el-descriptions-item>
      <el-descriptions-item :key="formData.DistrictCode" label="行政区划">
        {{ showAllName(formData.District) }}
      </el-descriptions-item>
      <el-descriptions-item :key="formData.EnvOrgCode" label="管理机构">
        {{ showAllName(formData.EnvOrg) }}
      </el-descriptions-item>
      <el-descriptions-item label="联系人">
        {{ formData.ContactName }}
      </el-descriptions-item>
      <el-descriptions-item label="联系电话">
        {{ formData.ContactTel }}
      </el-descriptions-item>
      <el-descriptions-item label="法人">
        {{ formData.LegalPerson }}
      </el-descriptions-item>
      <el-descriptions-item label="法人联系电话">
        {{ formData.LegalPersonTel }}
      </el-descriptions-item>
      <el-descriptions-item label="经度">
        {{ formData.Longitude }}
      </el-descriptions-item>
      <el-descriptions-item label="纬度">
        {{ formData.Latitude }}
      </el-descriptions-item>
      <el-descriptions-item :key="formData.OwnerOrgCode" label="运维公司">
        {{ showAllName(formData.OwnerOrg) }}
      </el-descriptions-item>
      <el-descriptions-item label="安装日期">
        {{ formData.InstallDate }}
      </el-descriptions-item>
      <el-descriptions-item label="地址">
        {{ formData.Address }}
      </el-descriptions-item>
      <el-descriptions-item :key="formData.NEIndustryCode" label="行业">
        {{ showAllName(formData.NEIndustry) }}
      </el-descriptions-item>
    </el-descriptions>
    <div class="mt-[10px] flex justify-end">
      <el-button type="warning" @click="exportData">
        导出
      </el-button>
    </div>
  </div>
</template>
